package com.sora.basic.service.impl;

import com.sora.basic.mapper.BaseMapper;
import com.sora.basic.query.BaseQuery;
import com.sora.basic.service.IBaseService;
import com.sora.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：基础
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        baseMapper.del(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> pageQuery(BaseQuery query) {
        //1.先查询总条数totals
        Integer totals = baseMapper.queryCount(query);
        if(totals < 1){//如果总条数小于1（等于0），直接结束查询，可以提高响应速度
            return new PageList<>();
        }

        //2.再查询当前页的数据
        List<T> data = baseMapper.queryData(query);

        return new PageList<>(data, totals);
    }

    @Override
    public void batchDel(List<Long> ids) {
        baseMapper.batchDel(ids);
    }

}
